#
# -= Makefile for application compile =-
#
# Note:
# . This is the second part of two makefile components;
#   Please refer "Makefile" for the other part.
#
# Usage:
# . Name this file as "Makefile";
#   Put it in the same directory as application's source code.
# . Change the ROOT definition, if necessary;
#   Make it point to the root of the software tree.
# . Take application as a module, define the name in MOD;
#   There should be NO duplicated module names in the whole project.
# . List all files want to be compiled in ASMS and/or SRCS;
#   Including files, such as header files, must NOT be listed here.
# . List all library files this module may depends on in LIBS.
# . Give a new name in SMOD, if want to include sub-directories;
#   Write the linkage information at the end of file.
#

# Destination to the root of software tree
ROOT = ../../../../../..
DDK_BLSDK_DIR = ${ROOT}/ddk/blsdk
include ${ROOT}/src/path.def
include ${ROOT}/src/compiler.def

# Module Name
MOD = BOOT_LOADER3

ifdef DUAL_CPU
    BOOT = boot36f
else
    BOOT = boot36f_single
endif

ifdef _BOARD_DB_C3047_T2_01V01_
    SLOT = libslot_sm1668
else
    SLOT = libslot3602f
endif

ifdef DUAL_CPU
    BOOTCONFIG = boot
else
    BOOTCONFIG = boot_single_64M
endif

# Link Script File
ifeq ($(_MULTI_SECTION),_MULTI_SECTION_E)
LSCR = ldscript_ms.ld
else
LSCR = ldscript.ld
endif

# List of source files
	ASMS =
	SRCS = bootloader.c expand.c rsa_verify.c 


# List of library files
LIBS =

# List of dependent modules
DMOD =


_LIB_UI_CFG = 


# Release library
RMOD = LIB_C LIB_CHUNK LIB_LIB7ZIP BUS_I2C_SCB BUS_I2C_GPIO\
	  HLD_STO LLD_STO_FLASH_SQI BUS_SCI_UART\
	  LLD_IRC_M63 HLD_BASE HLD_PAN LIB_BOOTUPG3

ifdef _BOARD_DB_C3047_T2_01V01_
    RMOD += LLD_PAN_SM1668 BOOT_SLOT_SM1668
else
    RMOD += LLD_PAN_CH455 BOOT_SLOT3602F
endif


LMOD = 
SMOD =

RELPROJ = ${BASE_DIR}
RELLIB = libbl
SDKLIB = libboot


# Following lines are the common description for all projects.
# Do NOT modify anything, unless you know what you are doing.

OBJS = ${ASMS:.S=.o} ${SRCS:.c=.o}
SMOD_ = ${SMOD:=_}
DMOD_ = ${DMOD:=_}
RMOD_ = ${RMOD:=_}
LMOD_ = ${LMOD:=_}
SMOD__ = ${SMOD:=__}
DMOD__ = ${DMOD:=__}
RMOD__ = ${RMOD:=__}
LMOD__ = ${LMOD:=__}


all : ${MOD}.mk
	cp ${DDK_BLSDK_DIR}/$(BOOT).o ${LIB_DIR}/boot.o
	cp ${DDK_BLSDK_DIR}/libstandby36.a ${LIB_DIR}/libstandby.a
	cp ${DDK_BLSDK_DIR}/$(SLOT).a ${LIB_DIR}/libslot.a
	cp ${DDK_BLSDK_DIR}/liblog.a ${LIB_DIR}/
	cp ${DDK_BLSDK_DIR}/libboot36f.a ${LIB_DIR}/libboot.a
	cp ${ROOT}/ddk/lib7zip.a ${LIB_DIR}/
	cp ${BOOTCONFIG}.cfg ${LIB_DIR}/boot.cfg
	cp ${ROOT}/prj/tools/bmerger.exe ${LIB_DIR}/bmerger.exe
	${MAKE} -f ${MOD}.mk ${OBJS}
	cp -f ${OBJS} ${LIBS} ${LIB_DIR}
	echo ${OBJS} ${LIBS} \\ > ${LIB_DIR}/${MOD}.lst
	rm -f ${LIB_DIR}/list.mod
	cd ${ROOT}/src; \
	  ${MAKE} ${DMOD}
	cp -f ${LSCR} ${LIB_DIR}/lscript.ld
	cd ${LIB_DIR}; \
	  echo include ../src/path.def > ${MOD}.lk; \
	  echo include ../src/compiler.def >> ${MOD}.lk; \
	  echo NEED = \\ >> ${MOD}.lk; \
	    echo cat \\ > cat.sh; \
	    cat list.mod >> cat.sh; \
	    echo ${MOD}.lst \\ >> cat.sh; \
	    echo \>\> ${MOD}.lk >> cat.sh; \
	    sh < cat.sh; \
	    rm -f cat.sh
	cat Makefile >> ${LIB_DIR}/${MOD}.lk
	cd ${LIB_DIR}; \
	  ${MAKE} -f ${MOD}.lk link
		
${MOD}.mk : ${ASMS} ${SRCS}
	echo include ${ROOT}/src/path.def > $@
	echo include ${ROOT}/src/compiler.def >> $@
	cat Makefile >> $@
	${CC} ${CFLAGS} -M ${ASMS} ${SRCS} >> $@

#36f:
#		cd ${ROOT}/src/boot;\
#		${MAKE} 36f
clean :
	rm -f ${OBJS} ${MOD}.mk
	cd ${LIB_DIR}; \
	  ${MAKE} -f ${MOD}.lk cleanld; \
	  rm -f lscript.ld list.mod ${MOD}.lk; \
	  echo rm -f \\ > rm.sh; \
	  cat ${MOD}.lst >> rm.sh; \
	  sh < rm.sh; \
	  rm -f rm.sh ${MOD}.lst boot.o liblog.a libboot.a libstandby.a libslot.a boot.cfg bmerger.exe

new : clean
	cd ${ROOT}/src; \
	  ${MAKE} ${DMOD_} ${RMOD_}

rebuild: clean all

sdk_rel :
	rm -f ${LIB_DIR}/list.ddk
	cd ${ROOT}/src; \
	  ${MAKE} ${RMOD__}
	cd ${LIB_DIR}; \
	  echo NEED = \\ > ${SDKLIB}.mak; \
	  echo cat \\ > cat.sh; \
	  cat list.mod >> cat.sh; \
	  echo \>\> ${SDKLIB}.mak >> cat.sh; \
	  sh < cat.sh; \
	  rm -f cat.sh
	echo "#" >> ${LIB_DIR}/${SDKLIB}.mak
	echo all: >> ${LIB_DIR}/${SDKLIB}.mak
	echo "	${AR} -r ${SDKLIB}.a \$${NEED}" >> ${LIB_DIR}/${SDKLIB}.mak
	cd ${LIB_DIR}; \
	  ${MAKE} -f ${SDKLIB}.mak
	cp -f ${LIB_DIR}/${SDKLIB}.a ${DDK_BLSDK_DIR}/
	rm -f ${LIB_DIR}/${SDKLIB}.mak

release :
	rm -f ${LIB_DIR}/list.mod
	cd ${ROOT}/src; \
	  ${MAKE} ${RMOD}
	cd ${LIB_DIR}; \
	  echo NEED = \\ > ${RELLIB}.mak; \
	  echo cat \\ > cat.sh; \
	  cat list.mod >> cat.sh; \
	  echo \>\> ${RELLIB}.mak >> cat.sh; \
	  sh < cat.sh; \
	  rm -f cat.sh
	echo "#" >> ${LIB_DIR}/${RELLIB}.mak
	echo all: >> ${LIB_DIR}/${RELLIB}.mak
	echo "	${AR} -r ${RELLIB}.a \$${NEED}" >> ${LIB_DIR}/${RELLIB}.mak
#	sed -e "s/boot.o//" -e "s/glue.o//" ${LIB_DIR}/${RELLIB}.mak > ${LIB_DIR}/${RELLIB}.tmp
#	mv ${LIB_DIR}/${RELLIB}.tmp ${LIB_DIR}/${RELLIB}.mak
	cd ${LIB_DIR}; \
	  ${MAKE} -f ${RELLIB}.mak
#	cp -f ${LIB_DIR}/${RELLIB}.a ${LIB_DIR}/liblog.a ${LIB_DIR}/boot.o \
#	  ${LIB_DIR}/glue.o ${REL_DIR}/prj/${RELPROJ}/lib
	rm -f ${LIB_DIR}/${RELLIB}.mak
